# Find installed OpenCV2

# find include path
find_path(OpenCV2_INCLUDE_DIR
   NAMES opencv2/opencv.hpp
   DOC "OpenCV2 include path"
)

set(OpenCV2_INCLUDE_DIRS ${OpenCV2_INCLUDE_DIR})
set(OpenCV2_LIBRARIES)

if (OpenCV2_INCLUDE_DIR)
   set(OpenCV2_FOUND TRUE)
   if (NOT OpenCV2_FIND_QUIETLY)
      message(STATUS "Found OpenCV2: ${OpenCV2_INCLUDE_DIR}")
   endif()
else()
   set(OpenCV2_FOUND FALSE)
   if (OpenCV2_FIND_REQUIRED)
      message(FATAL_ERROR "Could not find OpenCV2")
   endif()
endif()

if (NOT OpenCV2_FIND_COMPONENTS)
   # include all components by default
   set(OpenCV2_FIND_COMPONENTS
      imgproc
      highgui
      ml
      video
      features2d
      calib3d
      objdetect
      contrib
      legacy
      flann
   )
endif()

# make sure core is included in the components
list(REMOVE_ITEM OpenCV2_FIND_COMPONENTS core)
list(APPEND OpenCV2_FIND_COMPONENTS core)

# find each component library and add it to OpenCV2_LIBRARIES
set(OpenCV2_LIBRARIES)
foreach(libname ${OpenCV2_FIND_COMPONENTS})
   find_library("OpenCV2_${libname}_LIBRARY"
      NAMES "opencv_${libname}"
      DOC "Library for OpenCV 2 ${libname} component."
   )

   if (OpenCV2_${libname}_LIBRARY)
      list(APPEND OpenCV2_LIBRARIES ${OpenCV2_${libname}_LIBRARY})
      if (NOT OpenCV2_FIND_QUIETLY)
         message(STATUS "Found OpenCV2 ${libname}: ${OpenCV2_${libname}_LIBRARY}")
      endif()
   else()
      if (OpenCV2_FIND_REQUIRED)
         message(FATAL_ERROR "Could not find OpenCV2 ${libname}")
      endif()
   endif()
endforeach()
